package swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;

import modele.Formation;
import java.awt.Dimension;

public class FormationListCellRenderer extends JPanel implements ListCellRenderer<Formation> {

	private static final long	serialVersionUID	= 1L;
	JLabel						labelId, labelCode, labelTitre;

	public FormationListCellRenderer() {

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 40, 100, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		labelId = new JLabel();
		labelId.setBackground(Color.ORANGE);
		labelId.setText("id");
		GridBagConstraints gbc_labelId = new GridBagConstraints();
		gbc_labelId.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelId.gridx = 0;
		gbc_labelId.gridy = 0;
		labelId.setBorder(new EmptyBorder(10, 10, 10, 10));
		add(labelId, gbc_labelId);

		labelCode = new JLabel();
		labelCode.setBackground(Color.ORANGE);
		labelCode.setText("code");
		GridBagConstraints gbc_labelCode = new GridBagConstraints();
		gbc_labelCode.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelCode.insets = new Insets(0, 0, 0, 0);
		gbc_labelCode.gridx = 1;
		gbc_labelCode.gridy = 0;
		labelCode.setBorder(new EmptyBorder(10, 10, 10, 10));
		add(labelCode, gbc_labelCode);

		labelTitre = new JLabel();
		labelTitre.setBackground(Color.ORANGE);
		labelTitre.setText("titre");
		GridBagConstraints gbc_labelTitre = new GridBagConstraints();
		gbc_labelTitre.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelTitre.gridx = 2;
		gbc_labelTitre.gridy = 0;
		labelTitre.setBorder(new EmptyBorder(10, 10, 10, 10));
		add(labelTitre, gbc_labelTitre);

		labelId.setOpaque(true);
		labelCode.setOpaque(true);
		labelTitre.setOpaque(true);

	}

	@Override
	public Component getListCellRendererComponent(JList<? extends Formation> list, Formation value, int index, boolean isSelected,
			boolean cellHasFocus) {

		String id = String.valueOf(value.getIdFormation());
		String code = value.getCode();
		String titre = value.getTitre();

		labelId.setText(id);
		labelCode.setText(code);
		labelTitre.setText(titre);

		if (isSelected) {

			/*
			Color c =  new Color(150, 150, 255);
			
			*/

			labelId.setBackground(Color.YELLOW);
			labelId.setForeground(Color.BLACK);
			labelCode.setBackground(Color.YELLOW);
			labelCode.setForeground(Color.BLACK);
			labelTitre.setBackground(Color.YELLOW);
			labelTitre.setForeground(Color.BLACK);

		} else {
			labelId.setBackground(list.getBackground());
			labelId.setForeground(list.getForeground());
			labelCode.setBackground(list.getBackground());
			labelCode.setForeground(list.getForeground());
			labelTitre.setBackground(list.getBackground());
			labelTitre.setForeground(list.getForeground());

		}

		setEnabled(true);
		setFont(list.getFont());
		setBackground(list.getBackground());
		setForeground(list.getForeground());
		return this;
	}
}
